package cn.com.HiSting1990.MyCompany;

import java.io.*;

public class MyIOTest1 {
    public static void main(String[] args) {
        String fileName = "C:\\Dev\\testFiles\\久久美剧网.txt";
//        readFileByBytes(fileName);
//        readFileByChars(fileName);
        readFileByLines(fileName);
    }

    public static void readFileByBytes(String fileName){
        File file = new File(fileName);
        InputStream inputStream = null;
        try {
            System.out.println("以字节为单位读取文件内容，一次读一个字节");
            inputStream = new FileInputStream(file);
            int tempbyte;
            while ((tempbyte = inputStream.read())!= -1){
                System.out.write(tempbyte);
            }
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }

        try {
            System.out.println("已字节为单位读取文件内容，一次读取多个字节");
            inputStream = new FileInputStream(file);
            //创建一个字节数组
            byte[] bytes = new byte[100];
            MyIOTest1.showAvailableBytes(inputStream);
            int byteread = 0;
            //读入多个字节到字节数组中，byteread是为一次读入的字节数
            while ((byteread = inputStream.read(bytes)) != -1){
                System.out.write(bytes,0,byteread);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {}
            }
        }
    }


    /**
     * 显示输入流中还剩的字节数
     */
    private static void showAvailableBytes(InputStream in) {
        try {
            System.out.println("当前字节输入流中的字节数为:" + in.available());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //以字符为单位读取文件，常用于读取文本，数字类型的文件
    public static void readFileByChars(String fileName){
        File file = new File(fileName);
        Reader reader = null;
        System.out.println("以字符为单位读取文件内容，一次读一个字节");
        try {
            reader = new InputStreamReader(new FileInputStream(file));
            int tempchar;
            while ((tempchar = reader.read()) !=-1){
                // 对于windows下，\r\n这两个字符在一起时，表示一个换行。
                // 但如果这两个字符分开显示时，会换两次行。
                // 因此，屏蔽掉\r，或者屏蔽\n。否则，将会多出很多空行。
                if ((char)tempchar != '\r'){
                    System.out.print((char)tempchar);
                }
            }
            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {}

        System.out.println("以字符为单位读取文件内容，一次读取多个字节");

        try {
            reader = new InputStreamReader(new FileInputStream(fileName));
            //创建char的数组
            char[] tempchars = new char[30];
            int charread = 0;
            while ((charread = reader.read(tempchars))!= -1){
//                if ((charread == tempchars.length) && (tempchars[tempchars.length-1] !='\r')){
//                    System.out.print(tempchars);
//                }else{
                    for (int i=0;i<charread;i++){
                        if (tempchars[i] == '\r'){
                            continue;
                        }else {
                            System.out.print(tempchars[i]);
                        }
                    }
                }
//            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //以行单位来读取文件内容，常用于读面向行的格式化文件
    public static void readFileByLines(String fileName){
        File file = new File(fileName);
        BufferedReader br = null;
        System.out.println("以行单位读取文件内容，一次读一行");
        try {
            int line = 1;
            br = new BufferedReader(new FileReader(file));
            String tempRead = null;
            while ((tempRead = br.readLine())!= null){
                System.out.println("line"+line+":"+tempRead);
                line++;
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
